//
//  SleepHeartRatePatternsView.swift
//  DaRings
//
//  Created by 易先生 on 2022/11/5.
//

import UIKit
import MYModule

class SleepHeartRatePatternsView: View {

    override func makeUI() {
        super.makeUI()
        self.addSubview(titleLabel)
        self.addSubview(subitTitleLabel)
        self.addSubview(iconImageView)
        self.addSubview(contentStackView)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        subitTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        iconImageView.snp.makeConstraints { make in
            make.top.equalTo(subitTitleLabel.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview()
        }
        
        contentStackView.snp.makeConstraints { make in
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    public func bind(to title: String, subitTitle: String, icon: String, contents: [String]) {
        
        titleLabel.text = title
        subitTitleLabel.text = subitTitle
        iconImageView.image = UIImage(named: icon)
        
        contentStackView.removeAllSubViews()
        contents.forEach { content in
            let contentLabel = Label()
            contentLabel.font = Fonts.systemFont(ofSize: 14)
            contentLabel.textColor = UIColor.assist_A9A9A9
            contentLabel.attributedText = content.toAttributed
                .font(Fonts.systemFont(ofSize: 14))
                .foregroundColor(UIColor.assist_A9A9A9)
                .paragraph(setup: {$0.lineSpacing = 6})
                .rawValue
            contentLabel.numberOfLines = 0
            contentStackView.addArrangedSubview(contentLabel)
        }
    }
    
    private lazy var titleLabel: Label = {
        let titlelabel = Label()
        titlelabel.font = Fonts.systemFont(ofSize: 16, weight: .semibold)
        titlelabel.textColor = UIColor.assist_222222
        titlelabel.numberOfLines = 0
        return titlelabel
    }()
    
    private lazy var subitTitleLabel: Label = {
        let subitTitleLabel = Label()
        subitTitleLabel.font = Fonts.systemFont(ofSize: 14, weight: .semibold)
        subitTitleLabel.textColor = UIColor.assist_A9A9A9
        subitTitleLabel.numberOfLines = 0
        return subitTitleLabel
    }()
    
    private lazy var iconImageView: ImageView = {
        let iconImageView = ImageView()
        return iconImageView
    }()
    
    private lazy var contentStackView: StackView = {
        let contentStackView = StackView()
        contentStackView.axis = .vertical
        contentStackView.spacing = 10
        return contentStackView
    }()
}
